package ar.activity;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.Window;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import ar.Location.TextToSpeak.TTSHelper;
import ar.Location.UI.WeatherAdapter;
import ar.Location.Weather_2.CurrentObservation;
import ar.Location.Weather_2.LocalWeather;
import ar.Location.Weather_2.LocalWeatherProvider;
import ar.activity.R.id;
import ar.activity.R.string;

public class LocalWeatherForecastActivity extends BaseActivity {
	public static LocalWeather localWeather = null;

	final char degree = (char) 0x00B0;

	public TextView txtViewCurrentCondition = null;
	public TextView txtViewForecastCondition = null;
	public TextView txtViewWeatherForecast = null;
	
	public TextView txtViewLocationName = null;
	public TextView txtViewDate = null;

	public TextView txtViewWeather = null;
	public TextView txtViewTemp = null;
	public TextView txtViewWind = null;

	public TextView txtViewPress = null;
	public TextView txtViewDewpoint = null;
	public TextView txtViewHumidity = null;
	public TextView txtViewVisibility = null;

	public ImageView imgViewWeather = null;

	public Gallery galleryWeather = null;
	public WeatherAdapter weatherAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);		
		setContentView(R.layout.weather_no_data_layout);
		super.onCreate(savedInstanceState);

		if (localWeather != null) {
			initView();
			updateWeatherUI(localWeather);
		}
	}

	private void initView() {
		// TODO Auto-generated method stub
		tvWhatScreen.setText(res.getString(string.weatherScreenName));
		
		setContentView(R.layout.local_weather_layout);
		
		txtViewCurrentCondition = (TextView)findViewById(R.id.lcCurrentCondition);
		txtViewForecastCondition = (TextView)findViewById(R.id.lcForecastCondition);
		txtViewWeatherForecast = (TextView)findViewById(R.id.txtViewWeatherForecast);

		txtViewLocationName = (TextView) findViewById(id.lcTxtViewLocationName);
		txtViewDate = (TextView) findViewById(id.lcTxtViewObservationTime);

		txtViewWeather = (TextView) findViewById(id.lcTxtViewWeather);
		txtViewTemp = (TextView) findViewById(id.lcTxtViewTemperature);
		txtViewWind = (TextView) findViewById(id.lcTxtViewWindString);

		txtViewPress = (TextView) findViewById(id.lcTxtViewPressure);
		txtViewDewpoint = (TextView) findViewById(id.lcTxtViewDewPoint);
		txtViewHumidity = (TextView) findViewById(id.lcTxtViewHumidity);
		txtViewVisibility = (TextView) findViewById(id.lcTxtViewVisibility);

		imgViewWeather = (ImageView) findViewById(id.lcImgViewWeather);

		galleryWeather = (Gallery) findViewById(id.lcGalleryWeather);
		weatherAdapter = new WeatherAdapter(this);
		galleryWeather.setAdapter(weatherAdapter);

		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		MarginLayoutParams mlp = (MarginLayoutParams) galleryWeather
				.getLayoutParams();
		mlp.setMargins(-(metrics.widthPixels / 2), mlp.topMargin,
				mlp.rightMargin, mlp.bottomMargin);
	}

	private void updateWeatherUI(LocalWeather lcWeather) {
		// TODO Auto-generated method stub
		if (lcWeather != null) {
			CurrentObservation currentObservation = lcWeather.currentObservation;

			String tempF = currentObservation.tempF;
			String tempC = currentObservation.tempC;
			String tempString = "Temperature: " + tempC + " " + degree + "C "
					+ "(" + tempF + " " + degree + "F" + ")";

			String pressMb = currentObservation.pressureMb;
			String pressIn = currentObservation.pressureIn;
			String pressString = "Pressure: " + pressMb + " Mb " + "("
					+ pressIn + " In" + ")";

			String dewPointF = currentObservation.dewPointF;
			String dewPointC = currentObservation.dewPointC;
			String dewPointString = "Dewpoint: " + dewPointC + " " + degree
					+ "C " + "(" + dewPointF + " " + degree + "F" + ")";

			String visibilityKm = currentObservation.pressureMb;
			String visibilityMi = currentObservation.pressureIn;
			String visibilityString = "Visibility: " + visibilityKm + " Km "
					+ "(" + visibilityMi + " mi" + ")";

			txtViewLocationName.setText(currentObservation.fullName);
			txtViewDate.setText(currentObservation.observationTime);

			txtViewWeather.setText("Weather: " + currentObservation.weather);
			txtViewTemp.setText(tempString);
			txtViewWind.setText("Wind: " + currentObservation.windString);

			txtViewPress.setText(pressString);
			txtViewDewpoint.setText(dewPointString);
			txtViewHumidity.setText("Relative humidity: "
					+ currentObservation.relativeHumidity);
			txtViewVisibility.setText(visibilityString);

			int resId = LocalWeatherProvider
					.getResId(localWeather.currentObservation.weather);
			if (resId == -1) {
				imgViewWeather.setImageBitmap(null);
				imgViewWeather.setVisibility(View.GONE);
			} else {
				imgViewWeather.setImageResource(resId);
			}
		}
	}
	
	@Override
	protected void onUpdateUI() {
		// TODO Auto-generated method stub
		txtViewCurrentCondition.setText(res.getString(string.currentCondition));
		txtViewForecastCondition.setText(res.getString(string.forecastCondition));
		txtViewWeatherForecast.setText(res.getString(string.weatherTitle));
	}
}
